package com.jony.actionbarccustom;

import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //自定义ActionBar
        final ActionBar actionBar = getActionBar();
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        actionBar.setCustomView(R.layout.actionbar);//自定义ActionBar布局
        actionBar.getCustomView().setOnClickListener(new OnClickListener() {//监听事件
            @Override
            public void onClick(View arg0) {
                switch (arg0.getId()) {
                case R.id.back:
                    showToast(R.string.finish);
                    finish();
                    break;
                default:
                    break;
                }
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.actionbar_menu, menu);
        //使用代码来动态控制MenuItem的显示状态 or XML定义MenuItem的显示状态 
/*        MenuItem share = menu.findItem(R.id.share);
        MenuItem undo = menu.findItem(R.id.undo);
        MenuItem redo = menu.findItem(R.id.redo);
        MenuItem save = menu.findItem(R.id.save);
        share.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        undo.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        redo.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        save.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);*/
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.undo:
            showToast(R.string.undo);
            break;
        case R.id.redo:
            showToast(R.string.redo);
            break;
        case R.id.save:
            showToast(R.string.save);
            break;
        case R.id.share:
            showToast(R.string.share);
            break;
        default:
            break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void showToast(int msg){
        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
    }
}
